﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using XcoAppSpaces.Contracts.CcrExtensions;
using XcoAppSpaces.Core;
using Microsoft.Ccr.Core;
using System.Threading;
using NUnit.Framework;
using XcoAppSpaces.Serialization.Json;
using PortExtensions=XcoAppSpaces.Core.PortExtensions;

namespace test.XcoAppSpaces.Core
{
	[TestFixture]
	public class testRemotePortInfo
	{
		[Test]
		[Repeat(10)]
		public void TestRemotePortInfoCorrect()
		{
			using (XcoAppSpace space1 = new XcoAppSpace("tcp.port=8000"),
			                   space2 = new XcoAppSpace("tcp.port=8001"))
			{
				//the remote port info is assigned through a ccr task, therefore this test is for making sure
				//that the port info is always available, even when accessed immediately in the receiving method

				var port = new Port<Port<int>>();
				space1.RunWorker(port);
				XcoPortInfo info = null;
				space1.Receive(port, msg => info = PortExtensions.GetPortInfo(msg)); 
				var worker = space2.ConnectWorker<Port<Port<int>>>(Environment.MachineName + ":8000");

				var localPort = new Port<int>();
				worker.Post(localPort);
				Thread.Sleep(200);

				Assert.IsTrue(info.IsRemotePort);
				Assert.AreEqual(Environment.MachineName.ToLower() + ":8001", info.RemoteAddress);

			}
		}
	}
}
